//
//  WelocmViewController.swift
//  01-界面搭建
//
//  Created by Apple on 15/11/8.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SDWebImage
class WelocmViewController: UIViewController {
    //MARK: - 视图生命周期
    override func loadView() {
        view = BackImageView
        setUpUI()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //异步加载用户头像
        iconView.sd_setImageWithURL(UserAccountViewModel.shardViewModel.avatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //更改约束
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height+200)
        }
        //设置动画
        label.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            //修改所有“可动画”属性
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (_) -> Void in
                        //切换视图  发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object:nil)
                        print("OK")
                        
                })
                
        }
    }
    //MARK: - 懒加载控件
    private lazy var BackImageView:UIImageView = UIImageView(ImageName: "ad_background")
    private lazy var iconView:UIImageView = {
        let iv = UIImageView(ImageName: "avatar_default_big")
        //设置圆形剪裁
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        return iv
    }()
    private lazy var label:UILabel = UILabel(Labeltext: "欢迎归来" + (UserAccountViewModel.shardViewModel.account?.screen_name)!, Labelfont: 18)
}
//MARK: - 添加控件
extension WelocmViewController {
    private func setUpUI() {
        //1.添加控件
        view.addSubview(iconView)
        view.addSubview(label)
        //自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.height.equalTo(90)
        }
        label.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    
}